//
//  LoginViewController.m
//  随便点
//
//  Created by qfsoft on 15-3-10.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "LoginViewController.h"
#import "MembersViewController.h"
#import "AppDelegate.h"
#define KLogin @"http://www.51anys.com:8080/waimaiwang/user/login"
@interface LoginViewController (){
    NSMutableArray *array;
    AppDelegate *app;
    UIAlertView *_alert;
    NSUserDefaults *defaults;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    self.passWord.secureTextEntry = YES;
    [self.automatic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"checkout.png"]]];
    self.automatic.layer.borderColor = [[UIColor grayColor] CGColor];
    self.automatic.layer.borderWidth = 1;
    self.automatic.userInteractionEnabled=YES;
    UITapGestureRecognizer *click=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(choices:)];
    [self.automatic addGestureRecognizer:click];
    [self read];
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)choices:(UIGestureRecognizer *)sender{
    if (_automatic.tag==1) {
        [self TuPianclick1];
        _automatic.tag=0;
    }
    else{
        [self TuPianclick];
        _automatic.tag=1;
    }
}
-(void)TuPianclick{
    _automatic.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"check.png"]];
    [defaults setObject:@"YES" forKey:@"on_off"];
}
-(void)TuPianclick1{
    _automatic.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"checkout.png"]];
    [defaults setObject:@"NO" forKey:@"on_off"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}


- (IBAction)login:(id)sender {
    if ([self.phone.text isEqualToString:@"" ]||[self.passWord.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号或者密码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
            [self Login];
        [defaults setObject:self.phone.text forKey:@"yonghu"];
        
        if ([[defaults objectForKey:@"on_off"] isEqual:@"YES"]) {
            [defaults setObject:self.passWord.text forKey:@"mima"];
        }
        else{
            
            [defaults setObject:@"" forKey:@"mima"];
        }
        // 3.立刻同步
        [defaults synchronize];
    }
}
- (void)read {
    defaults = [NSUserDefaults standardUserDefaults];
    
    if ([[defaults objectForKey:@"on_off"] isEqual:@"YES"]) {
        
        self.automatic.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"check.png"]];
        
    }
    else
    {
        self.automatic.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"checkout.png"]];
    }
    self.phone.text= [defaults objectForKey:@"yonghu"];
    
    self.passWord.text= [defaults objectForKey:@"mima"];
    
    if (![self.passWord.text isEqualToString:@""]) {
    }
    
    
}
-(void)Login{
    NSURL *url=[NSURL URLWithString:KLogin];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"userId=%@&password=%@",self.phone.text,self.passWord.text];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:array waitUntilDone:YES];
        app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        app.person = array;
        if (array == nil) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号码与密码不匹配" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            _alert=alert;
            [alert show];
        }else{
            [self performSegueWithIdentifier:@"login" sender:nil];
        }
    }];
}
-(void)refreshData:(NSArray *)sender{
}
- (IBAction)forget:(id)sender {
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.phone resignFirstResponder];
    [self.passWord resignFirstResponder];
}

@end
